﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using AjaxControlToolkit;
using System.Text;
using DAL;
using System.Web.Configuration;

public partial class Laptop : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    string connectionString = WebConfigurationManager.ConnectionStrings["DAL.Properties.Settings.DigitalWorldConnectionString"].ConnectionString;
    static string strConnection = "server =.;database = DigitalWorld;uid=sa;pwd=123";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["CateID"] == null)
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                if (Request.QueryString["SupID"] == null)
                {
                    string CatID = Request.QueryString["CateID"].ToString();
                    lbSelected.Text = "0 item(s)";
                    GetCateName(CatID);
                    getProductByCategory(CatID);
                    LoadCountItem();
                }
                else
                {
                    string CatID = Request.QueryString["CateID"].ToString();
                    string SupName = Request.QueryString["SupID"].ToString();
                    lbSelected.Text = "0 item(s)";
                    GetCateName(CatID);      
                    getProductByCatgoryAndSupplier(CatID, SupName);
                    LoadCountItem();
                }
            }
        }
        lblUserOnline.Text = Session["online"].ToString();

        lblNumVisitor.Text = Application["count_visit"].ToString();
        
    }
    #region Hàm lấy tên chủng loại
    /// <summary>
    /// Hàm lấy tên chủng loại
    /// </summary>
    /// <param name="CatID"></param>
    public void GetCateName(string CatID)
    {
        var danhsach = db.categories.SingleOrDefault(c => c.categoryid.Equals(CatID));
        lbCategoryname.Text = danhsach.categoryname;
    }
    #endregion

    void LoadCountItem()
    {
        DataTable dtCart = Session["Cart"] as DataTable;
        if (dtCart.Rows.Count > 0)
        {
            lbSelected.Text = dtCart.Compute("SUM(Quantity)", "").ToString() + " item(s)";
        }
    }

    public void getProductByCategory(string CatID) {
        var danhsach = db.products.Where(p => p.categoryid.Equals(CatID)).OrderByDescending(p=>p.productid);
        CollectionPager1.PageSize = 4;
        CollectionPager1.DataSource = danhsach.ToList();
        CollectionPager1.BindToControl = dl_Information;
        dl_Information.DataSource = CollectionPager1.DataSourcePaged;
    }
    public void getProductByCatgoryAndSupplier(string CatID, string SupName){
        var danhsach = from p in db.products
                       join c in db.categories on p.categoryid equals c.categoryid
                       join s in db.suppliers on p.supplierid equals s.supplierid
                       where p.categoryid == CatID && s.companyname == SupName
                       orderby p.productid descending
                       select p;
        CollectionPager1.PageSize = 4;
        CollectionPager1.DataSource = danhsach.ToList();
        CollectionPager1.BindToControl = dl_Information;
        dl_Information.DataSource = CollectionPager1.DataSourcePaged;
    }
    protected void dl_Information_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
                    e.Item.ItemType == ListItemType.AlternatingItem)
        {
            PopupControlExtender pce = e.Item.FindControl("PopupControlExtender1") as PopupControlExtender;

            string behaviorID = "pce_" + e.Item.ItemIndex;
            pce.BehaviorID = behaviorID;

            Image img = (Image)e.Item.FindControl("Image1");

            string OnMouseOverScript = string.Format("$find('{0}').showPopup();", behaviorID);
            string OnMouseOutScript = string.Format("$find('{0}').hidePopup();", behaviorID);

            img.Attributes.Add("onmouseover", OnMouseOverScript);
            img.Attributes.Add("onmouseout", OnMouseOutScript);
        }
    }
    [System.Web.Services.WebMethodAttribute(),
  System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetDynamicContent(string contextKey)
    {
        DigitalWorldDataContext db = new DigitalWorldDataContext();
        product product = db.products.SingleOrDefault(p => p.productid.Equals(int.Parse(contextKey)));
        supplier s = db.suppliers.SingleOrDefault(s1 => s1.supplierid.Equals(product.supplierid));

        StringBuilder b = new StringBuilder();

        b.Append("<table style='background-color:#c1ce56; border: #336699 3px solid; ");
        b.Append("width:350px; font-size:13px; font-family:Arial;' cellspacing='0' cellpadding='3'>");

        b.Append("<tr><td colspan='3' style='background-color:#336699; color:white;'>");
        b.Append("<b>Thông tin sản phẩm</b>");
        b.Append("</td></tr>");
        b.Append("<tr>");
        b.Append("<td style='width:300px;' align='left' colspan='3'><b>Tên sản phẩm : </b><b>" + product.productname + "</b></td>");
        b.Append("</tr>");
        b.Append("<tr>");
        b.Append("<td style='width:300px;' align='left' colspan='3'><b>Hãng sản xuất</b> : " + s.companyname + "</td>");
        b.Append("</tr>");
        b.Append("<tr>");
        decimal Price = decimal.Parse(product.unitprice_sell.ToString());
        b.Append("<td style='width:300px;' align='left' colspan='3'><b>Price</b> : " + Price.ToString("#,###,###") + " VNĐ</td>");
        b.Append("</tr>");
        b.Append("<tr>");
        string pathImage = product.photopath;
        string imgName = pathImage.Substring(pathImage.LastIndexOf("/") + 1);
        b.Append("<td valign ='top' style='width:70px;' align='center'>  <img align='left' width='70px' height='70px'  alt='' src='Image/Products/" +
            imgName + "'/></td><td valign ='top' style='width:230px;' colspan='2' align='left'>" + product.description + "</td>");

        b.Append("</tr>");
        b.Append("</table>");

        return b.ToString();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Tim-kiem.aspx?tk=" + txtsearch.Text);
    }
}